/**
 * LVL.Levitator - A component that, when powered, levitates above the ground.
 */

define(['blacksmith/game', 'lvl/config', 'lvl/component', 'lvl/components'], function() {


var vec3 = new THREE.Vector3(),
    springForce



/**
 * Constructs an LVL.Levitator instance.
 * 
 * @param {object} args
 * @constructor
 * @extends LVL.Component
 */
LVL.Levitator = function(args) {
    
    args = args || {}
    args.name = args.name || 'LEVITATOR'
    args.width = (args.width !== undefined) ? args.width : 100
    args.height = (args.height !== undefined) ? args.height : 20
    args.depth = (args.depth !== undefined) ? args.depth : 100
    args.colour = (args.colour !== undefined) ? args.colour : 0x555555
    args.mass = (args.mass !== undefined) ? args.mass : 10
    
    LVL.Component.call(this, args)
    
    this.hoverMaxHeight = 5 * args.height
    this.quarterForce = 0.25 * this.mass
    this.lt = new THREE.Vector3()
    this.rt = new THREE.Vector3()
    this.lb = new THREE.Vector3()
    this.rb = new THREE.Vector3()
    this.lastAngularUpdate = 0
    
}

LVL.Levitator.prototype = Object.create(LVL.Component.prototype)



/**
 * Updates component state.
 */
LVL.Levitator.prototype.update = function() {
    
    LVL.Component.prototype.update.call(this)
    
    if (!this.root.body)
        return
    
    if (this === this.root) {
        
        this.body.angularDamping = 0.1
        this.body.linearDamping = 0.1
        
        this.body.force.y += this.totalmass * CONFIG.gravity
        
    }
    
    
    if (GAME.time - this.lastAngularUpdate > 35) {
        
        this.lastAngularUpdate = GAME.time
        
        this.lt.x = -60
        this.lt.y = 0
        this.lt.z = -60
        
        this.rt.x = 60
        this.rt.y = 0
        this.rt.z = -60
        
        this.lb.x = -60
        this.lb.y = 0
        this.lb.z = 60
        
        this.rb.x = 60
        this.rb.y = 0
        this.rb.z = 60
        
        this.mesh.localToWorld(this.lt)
        this.mesh.localToWorld(this.rt)
        this.mesh.localToWorld(this.lb)
        this.mesh.localToWorld(this.rb)
        
        this.lt.f = this.quarterForce * (this.hoverMaxHeight - this.lt.y)
        this.rt.f = this.quarterForce * (this.hoverMaxHeight - this.rt.y)
        this.lb.f = this.quarterForce * (this.hoverMaxHeight - this.lb.y)
        this.rb.f = this.quarterForce * (this.hoverMaxHeight - this.rb.y)
        
        this.applyAngularForce(0, this.lt.f, 0, this.lt.x, this.worldPosition.y, this.lt.z)
        this.applyAngularForce(0, this.rt.f, 0, this.rt.x, this.worldPosition.y, this.rt.z)
        this.applyAngularForce(0, this.lb.f, 0, this.lb.x, this.worldPosition.y, this.lb.z)
        this.applyAngularForce(0, this.rb.f, 0, this.rb.x, this.worldPosition.y, this.rb.z)
        
    }
    
}



/**
 * Register this component.
 */
LVL.Components.register('LEVITATOR', LVL.Levitator,  ['LEVITATOR'], function() {
    
    GAME.Camera.object.position.set(0, 80, 150)
    GAME.panUp(-30)
    
    var c = new LVL.Levitator({angle:Math.PI*0.2})
    GAME.add(c)
    
    GAME.Renderer.render(GAME.Scene, GAME.Camera.object)
    var componentImage = GAME.Renderer.domElement.toDataURL("image/png")
    
    GAME.remove(c)
    
    GAME.panUp(30)
    
    return componentImage
    
})



})
